# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2023年02月23日
"""

import docx
from docx.shared import Cm,Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

def setAttrs(rootElement,elements,attrs):
	for element in elements:
		myElement=rootElement.find(docx.oxml.ns.qn(element))
		if myElement is None:
			myElement=docx.oxml.OxmlElement(element)
			rootElement.append(myElement)
		for key in attrs:
			myElement.set(docx.oxml.ns.qn(key),attrs[key])

# 自定义单元格边框颜色的函数
def setCellBorderColor(row,col,myColor):
	myTcPr=myTable.cell(row,col)._tc.get_or_add_tcPr()
	myTcBorders=myTcPr.first_child_found_in('w:tcBorders')
	if myTcBorders is None:
		myTcBorders=docx.oxml.OxmlElement('w:tcBorders')
		myTcPr.append(myTcBorders)
	for myEdge in ('left','right','bottom','top'):
		myEdgeData={'color':myColor}
		if myEdgeData:
			myTag='w:{}'.format(myEdge)
			myElement=myTcBorders.find(docx.oxml.ns.qn(myTag))
			if myElement is None:
				myElement=docx.oxml.OxmlElement(myTag)
			myTcBorders.append(myElement)
			myElement.set(docx.oxml.ns.qn('w:{}'.format('color')),str(myEdgeData['color']))


myDocument=docx.Document('新员工.docx')
myTable=myDocument.tables[0]
for i in range(len(myTable.rows)):
	for j in range(len(myTable.columns)):
		setCellBorderColor(i,j,"#ff0000")


myDocument.save('02散文名篇.docx')


